package com.meihuak.cloud_ordering.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.meihuak.cloud_ordering.bean.Business;
import com.meihuak.cloud_ordering.bean.Dish;
import com.meihuak.cloud_ordering.bean.OrderTable;
import com.meihuak.cloud_ordering.bean.OrderChild;
import com.meihuak.cloud_ordering.service.BusinessService;
import com.meihuak.cloud_ordering.service.DishService;
import com.meihuak.cloud_ordering.service.OrderChildService;
import com.meihuak.cloud_ordering.service.OrderService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 韩迪
 * @date 2022/03/26 16:00
 **/

@Slf4j
@Controller
@RequestMapping("/order")
public class OrderContorller {
    private static Integer orderno = 1;

    @Autowired
    OrderService orderService;
    @Autowired
    OrderChildService orderChildService;
    @Autowired
    DishService dishService;
    @Autowired
    BusinessService businessService;
//http://localhost:8080/order/createorder?stu_id=1&dishmap={{2:{1}}}&business_id=1
    @ResponseBody
    @RequestMapping("/createorder")
    public Map<String,Object>  createOrderChild(@RequestParam (value = "stu_id")Integer stu_id,
                                                @RequestParam (value = "business_id")Integer business_id,
                                                @RequestParam (value = "price")Double price,
                                                @RequestParam (value = "beizhu")String beizhu,
                                                @RequestParam (value = "dishlist")String dishlist
                                                ){
        long orderno = orderService.getOrderMaxKeyId()+1;
        OrderTable orderTable = new OrderTable(orderno,stu_id,business_id,0,new Date(),null,price,beizhu);
        orderService.save(orderTable);
        JSONArray dishes = JSONObject.parseArray(dishlist);
        for (Object dish : dishes) {
            String s = dish.toString();
            JSONObject dishmap = JSONObject.parseObject(s);
            Double priceDoubleValue;
            if(dishmap.get("price") instanceof BigDecimal){
                priceDoubleValue = ((BigDecimal)dishmap.get("price")).doubleValue();
            }else{
                priceDoubleValue = ((Integer)dishmap.get("price")).doubleValue();
            }


            OrderChild child = new OrderChild(null, orderno, (int) dishmap.get("dishid")
                    , (int) dishmap.get("count"), priceDoubleValue, (String) dishmap.get("dishname"));
            orderChildService.save(child);
        }
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("msg","成功");

        return retMap;
    }


    @ResponseBody
    @RequestMapping("/getorder")
    public Map<String,Object> getOrder(@RequestParam(value = "name",required = false)String name,
                                       @RequestParam(value = "id",required = false)Integer id
//                                        @RequestBody HashMap<String,Object> map
                                        ){
        Map<String,Object> selMap = new HashMap<>();
        if (name.equals("business")){
            selMap.put("business_id",id);

        }else if (name.equals("student")){
            selMap.put("stu_id",id.longValue());
        }
        List<OrderTable> list = orderService.listByMap(selMap);
        Map<String,Object> retMap = new HashMap<>();
        List<Map<String,Object>> lists =  new ArrayList<>();
        for (OrderTable orderTable : list){
            if(orderTable.getFlag()==1){
                continue;
            }
            List<OrderChild> children =
                    orderChildService.getOrderChiledByOrderId(orderTable.getId());
            List<Object> dishlist = new ArrayList<>(children);

            //获取取餐时间
            Integer time = orderService.getOrderWaitTime(orderTable.getId());
            Date fintime = new Date();

            fintime.setTime(orderTable.getCreateDate().getTime()+(time.longValue()*60*1000));
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            String ftime = dateformat.format(fintime);




            //创建订单
            Map<String,Object> ordermap = new HashMap<>();
            Business business = businessService.getById(orderTable.getBusinessId());
            ordermap.put("orderinfo",orderTable);
            ordermap.put("business_id",business.getId());
            ordermap.put("business_name",business.getName());
            ordermap.put("dishlist",dishlist);
            ordermap.put("fintime",ftime);
            lists.add(ordermap);
        }
        retMap.put("msg","成功获取");
        retMap.put("data",lists);
        return retMap;
    }

    @ResponseBody
    @RequestMapping("/finishorder")
    public Map<String,Object> getOrder(@RequestParam(value = "orderid",required = false)int orderid){
        Long oid = Integer.toUnsignedLong(orderid);
        orderService.finishOrder(oid);
        Map<String,Object> retMap = new HashMap<>();
        retMap.put("msg","成功获取");
        return retMap;



    }




}
